package com.lwh.springboot.controller;
import com.lwh.springboot.pojo.entity.CarType;
import com.lwh.springboot.service.ICarTypeService;
import com.lwh.springboot.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/carType")
public class CarTypeController {
    @Autowired
    private ICarTypeService carTypeService;

    @RequestMapping("/toUpdate")
    public String toUpdate(Integer id, Model model){
        CarType carType=carTypeService.selectById(id);
        model.addAttribute("carType",carType);
        return "/car_type/update";
    }
    @RequestMapping("/update")
    public String update(CarType carType){
        carTypeService.update(carType);
        return "redirect:/carType/selectByPage";
    }
    @RequestMapping("toAdd")
    public String toAdd(){
        return "/car_type/add";
    }
    @RequestMapping("/add")
    public String add(CarType carType){
        carTypeService.add(carType);
        return "redirect:/carType/selectByPage";
    }
    @RequestMapping("/deleteById")
    public String deleteById(Integer id){
        carTypeService.deleteById(id);
        return "redirect:/carType/selectByPage";
    }
    @RequestMapping("/deleteAll")
    public String deleteAll(Integer[] ids) {
        carTypeService.deleteAll(ids);
        return "redirect:/carType/selectByPage";
    }
    @RequestMapping("/selectByPage")
    public String selectByPage(@RequestParam(defaultValue = "1") Integer pageNo,
                               @RequestParam(defaultValue = "5") Integer pageSize, Model model) {
        System.out.println("CarTypeController.selectByPage");
        PageInfo pageInfo = carTypeService.selectByPage(pageNo, pageSize);

        //把pageInfo数据放到内存里面
        model.addAttribute("pageInfo", pageInfo);
        return "/car_type/list";
    }
}
